import java.io.*;
import java.util.Scanner;

class ConsoleShop {
    private ProductService productService = new ProductService();
    private Scanner scanner = new Scanner(System.in);

    public void start() {
        while (true) {
            showMenu();
            int choice = getInput();
            processChoice(choice);
        }
    }

    private void showMenu() {
        System.out.println("\n=== 商品管理系统 ===");
        System.out.println("1. 添加商品");
        System.out.println("2. 查看商品");
        System.out.println("3. 更新商品");
        System.out.println("4. 删除商品");
        System.out.println("5. 显示所有商品");
        System.out.println("99. 备份数据 (管理员)");
        System.out.println("0. 退出系统");
        System.out.print("请选择操作: ");
    }

    private int getInput() {
        try {
            return Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    private void processChoice(int choice) {
        switch (choice) {
            case 1:
                addProduct();
                break;
            case 2:
                viewProduct();
                break;
            case 3:
                updateProduct();
                break;
            case 4:
                deleteProduct();
                break;
            case 5:
                showAllProducts();
                break;
            case 99: // 隐藏的管理员选项 - 数据备份
                backupData();
                break;
            case 0:
                exitSystem();
                break;
            default:
                System.out.println("无效选择，请重新输入");
        }
    }

    // 新增备份方法
    private void backupData() {
        System.out.print("确认备份数据？(y/n): ");
        String confirm = scanner.nextLine();
        if ("y".equalsIgnoreCase(confirm)) {
            try {
                backupDataFile();
                System.out.println("数据备份成功");
            } catch (IOException e) {
                System.err.println("数据备份失败: " + e.getMessage());
            }
        } else {
            System.out.println("取消备份");
        }
    }

    // 实际的文件备份逻辑
    private void backupDataFile() throws IOException {
        String sourceFile = "products.dat"; // 源文件
        String backupFile = "products.dat.bak"; // 备份文件

        File source = new File(sourceFile);
        if (!source.exists()) {
            throw new FileNotFoundException("源数据文件不存在: " + sourceFile);
        }

        try (FileInputStream fis = new FileInputStream(source);
             FileOutputStream fos = new FileOutputStream(backupFile)) {

            byte[] buffer = new byte[1024]; // 1KB缓冲区
            int bytesRead;

            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        }

        System.out.println("数据文件已备份到: " + backupFile);
    }

    // 原有的方法保持不变
    private void addProduct() {
        System.out.print("输入商品ID: ");
        String id = scanner.nextLine();
        System.out.print("输入商品名称: ");
        String name = scanner.nextLine();
        System.out.print("输入商品价格: ");
        double price = Double.parseDouble(scanner.nextLine());
        System.out.print("输入商品库存: ");
        int stock = Integer.parseInt(scanner.nextLine());

        Product product = new Product(id, name, price, stock);
        productService.addProduct(product);
        System.out.println("商品添加成功");
    }

    private void viewProduct() {
        System.out.print("输入商品ID: ");
        String id = scanner.nextLine();
        Product product = productService.getProduct(id);
        if (product != null) {
            System.out.println("商品信息: " + product);
        } else {
            System.out.println("商品不存在");
        }
    }

    private void updateProduct() {
        System.out.print("输入要更新的商品ID: ");
        String id = scanner.nextLine();
        Product product = productService.getProduct(id);
        if (product != null) {
            System.out.print("输入新商品名称: ");
            String name = scanner.nextLine();
            System.out.print("输入新商品价格: ");
            double price = Double.parseDouble(scanner.nextLine());
            System.out.print("输入新商品库存: ");
            int stock = Integer.parseInt(scanner.nextLine());

            product.setName(name);
            product.setPrice(price);
            product.setStock(stock);
            productService.productService.updateProduct(product);
            System.out.println("商品更新成功");
        } else {
            System.out.println("商品不存在");
        }
    }

    private void deleteProduct() {
        System.out.print("输入要删除的商品ID: ");
        String id = scanner.nextLine();
        productService.deleteProduct(id);
        System.out.println("商品删除成功");
    }

    private void showAllProducts() {
        Map<String, Product> products = productService.getAllProducts();
        if (products.isEmpty()) {
            System.out.println("没有商品数据");
        } else {
            System.out.println("所有商品:");
            for (Product product : products.values()) {
                System.out.println(product);
            }
        }
    }

    private void exitSystem() {
        System.out.println("正在保存数据...");
        productService.saveData();
        System.out.println("谢谢使用，再见！");
        scanner.close();
        System.exit(0);
    }

    public static void main(String[] args) {
        new ConsoleShop().start();
    }
}